package com.czollmann.android.activities;

import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.czollmann.android.R;

public class SettingsBaseActivity extends Activity {
  public boolean hasChanged = false;

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.settings_menu, menu);
    return true;
  }

  private void closeSettings() {
    if (hasChanged) {
      Intent refreshIntent = new Intent(this, StartupActivity.class);
      this.startActivity(refreshIntent);
      this.finish();
    } else {
      this.finish();
    }
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_exit :
        closeSettings();
        return true;
      default :
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
      closeSettings();
    }
    return super.onKeyDown(keyCode, event);
  }
}
